<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<!--App自定义的css-->
		<link rel="stylesheet" type="text/css" href="../css/mui.picker.min.css" />
		<style>
			html,
			body {
				background-color: #efeff4;
				overflow: hidden;
			}
			
			.mui-locker {
				margin: 35px auto;
			}
			
			#forget {
				display: block;
				margin-top: 10px;
				text-align: center;
			}
			#pic {
				width: 100px;
				height: 100px;
				margin: 20px auto;
				cursor: pointer;
			}
			
			#pic2 {
				width: 100px;
				height: 100px;
				margin: 20px auto;
				cursor: pointer;
			}
			
			#pic1 {
				width: 100px;
				height: 100px;
				margin: 20px auto;
				cursor: pointer;
			}
			
			#pic2 {
				width: 100px;
				height: 100px;
				margin: 20px auto;
				cursor: pointer;
			}
			
			#pic3 {
				width: 100px;
				height: 100px;
				margin: 20px auto;
				cursor: pointer;
			}
			
			h5 {
				margin-top: 10px;
			}
			span{
				 font-weight: normal;
				 margin-top: 10px;
				line-height: 1em;
				display:block;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav" style="height: 55px;">
			<div style="margin-top: 15px;">
			<a class="mui-icon mui-icon-left-nav mui-pull-left mui-action-back"></a>
			<h1 class="mui-title">返工</h1>
			</div>
		</header>
		<div id="" style="height: 50px;"></div>
		<div id="" style="background: white;height: 250px;">
			<div>
				<!--<h5>请叙述需要返工的原因：</h5>-->
				<textarea id="rework_resone" name="a" style="width:100%;height:100px;border: none;" placeholder="请叙述需要返工的原因"></textarea>
			</div>
			<div id="imgshow" style="background: white;">
				<form id="uploadForm" enctype="multipart/form-data">
					<img id="pic1" src="../images/iconfont-tianjia.png" style="display: ">
					<input id="upload1" src="../images/iconfont-tianjia.png" name="file1" accept="image/*" type="file" style="display: none" />
					<img id="pic2" src="../images/iconfont-tianjia.png" style="display: none">
					<input id="upload2" src="../images/iconfont-tianjia.png" name="file2" accept="image/*" type="file" style="display: none" />
					<img id="pic3" src="../images/iconfont-tianjia.png" style="display: none">
					<input id="upload3" src="../images/iconfont-tianjia.png" name="file3" accept="image/*" type="file" style="display: none" />
			</div>
		</div>

		<!--<div style="background: white;height: 60px;margin-top: 20px;">
			<span style="float: left;width: 100%;margin-left: 10%;">发送至：</span>
			<span id="people" style="float: right;margin-right: 10%;">李师傅</span>
		</div>-->
		<div class="mui-button-row" style="margin-top: 10px;">
			<button id="submit1" class="mui-btn-primary" style="width: 30%;margin-top: 20px;" type="button" onclick="upload()" style="display: none;">确定</button>
		    <a type="submit" id="submit2"  style="display: none ;" > 返工提交成功,返回到首页</button>
		</div>
		<script src="../js/urlconfiguration.js"></script>
		<script src="../js/mui.min.js"></script>
		<script src="../js/jquery-1.6.2.min.js"></script>
		<script src="../js/mui.picker.min.js"></script>
		<script src="http://lib.sinaapp.com/js/jquery/1.12.4/jquery-1.12.4.min.js"></script>
		<script type="text/javascript">
			var files1 = [];
			var files2 = [];
			var files3 = [];
			var old_back = mui.back;

			// 上传图片和信息
			function upload() {
				mui.plusReady(function() {
					var self = plus.webview.currentWebview();
					var jobitem_user_id = self.jobitem_user_id;
					var server = url_3 + "rework.do?"
					
					console.log(jobitem_user_id);
					var rework_resone = document.getElementById("rework_resone").value;
					if((files1.length <= 0&&files2.length <= 0&&files3.length <= 0) || rework_resone.length<=0) {
			plus.nativeUI.alert("请添加照片或返工原因！");
return;
}
var wt = plus.nativeUI.showWaiting("正在上传...\n", {

					});
					var task = plus.uploader.createUpload(server, {
							method: "POST"
						},
						function(t, status) { //上传完成
							if(t.responseText == '{"flag":"0"}') {
								//							 w.close();
								mui.alert("失败");
							}
							if(t.responseText == '{"flag":"1"}') {
								mui.alert("成功");
								wt.close();
								$("#submit1").attr("style", "display:none;")
								//$("#submit2").attr("style", "display:;")
									old_back();
									var list = plus.webview.currentWebview().opener();
									//触发父页面的自定义事件(refresh),从而进行刷新
									mui.fire(list, 'tomypublish');
									//返回true,继续页面关闭逻辑
									return true;
							} else {
								wt.close();
								mui.alert("上传失败：" + status);
							}
						}
					);
					task.addData("memo", rework_resone);
					task.addData("jobitem_user_id", jobitem_user_id.toString());
					var f1 = files1[files1.length - 1];
					var f2 = files2[files2.length - 1];
					var f3 = files3[files3.length - 1];
					if(files1.length>0){
					task.addFile(f1.path, {
						key: "img_url1"
					});
					}else{ 
						task.addFile("null", {
						key: "img_url1"
					});}
					if(files2.length>0){
					task.addFile(f2.path, {
						key: "img_url2"
					});
					}else{ 
						task.addFile("null", {
						key: "img_url2"
					});
					}
					if(files3.length>0){
					task.addFile(f3.path, {
						key: "img_url3"
					});
					}else{ 
						task.addFile("null", {
						key: "img_url3"
					});
					}
					task.start();
				})
			}

			function photo(id) {
				if(mui.os.plus) {
					var a = [{
						title: "拍照"
					}, {
						title: "从手机相册选择"
					}];
					plus.nativeUI.actionSheet({
						title: "照片上传",
						cancel: "取消",
						buttons: a
					}, function(b) { /*actionSheet 按钮点击事件*/
						switch(b.index) {
							case 0:
								break;
							case 1:
								appendByCamera(id); /*拍照*/
								break;
							case 2:
								appendByGallery(id); /*打开相册*/
								break;
							default:
								break;
						}
					})
				}
			}
			// 拍照添加文件
			function appendByCamera(id) {
				plus.camera.getCamera().captureImage(function(p) {
					plus.io.resolveLocalFileSystemURL(p, function(entry) { //判断本地文件是否存在？？
						var p = entry.toLocalURL(); //把拍照的目录路径，变成本地url路径
						appendFile(p,id);
					})
				});
			}

			// 从相册添加文件
			function appendByGallery(id) {
				plus.gallery.pick(function(p) {
					console.log(id+"id1")
					appendFile(p,id);
				});
			}
			// 向files[]中添加照片
			var index = 1;

			function appendFile(p,id) {
				console.log(id+"id")
				if(id==1){
				files1.push({
					name: "uploadkey" + index,
					path: p
				}); 
				$("#pic2").attr("style","display: ;")
				}else if(id==2){
				files2.push({
					name: "uploadkey" + index,
					path: p
				});
				$("#pic3").attr("style","display: ;")
				}else if(id==3){
				files3.push({
					name: "uploadkey" + index,
					path: p
				});
				}
				$("#pic" + id).attr("src", p);
				index++;
			}
			$(function() {
				$("#pic1").click(function() {
					photo(1);
				});
				$("#pic2").click(function() {
					photo(2);
				});
				$("#pic3").click(function() {
					photo(3);
				});
			});
		</script>


	</body>

</html>